using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Marlimer.Common;
using Marlimer.Comercial.Reportes.Crystals;
using Marlimer.Logical.Comercial; 

namespace Marlimer.Comercial.Reportes.Formularios
{
    public partial class frm_RegistroVentas : Form
    {
        List<InfoParamRpt> paramsrpt = new List<InfoParamRpt>();
        rptRegistroVentas RptRegistroVentas = new rptRegistroVentas();
        rptRegistroVentasFormato2 RptRegistroVentasFormato2 = new rptRegistroVentasFormato2();
        rptRepRegVentasResumen RptRepRegVentasResumen = new rptRepRegVentasResumen();
        rptVentasTotales RptVentasTotales = new rptVentasTotales(); 
        
        public frm_RegistroVentas()
        {
            InitializeComponent();
        }

        private void frm_RegistroVentas_Load(object sender, EventArgs e)
        {
            dtpFecIni.Value = DateTime.Today;
            dtpFecFin.Value = DateTime.Today;

            this.Location = new Point(0, 0);
        }

        private void CargarReporte()
        {
            //verificamos que se ingresen todos los datos.
            //establecemos los parametros
            paramsrpt.Clear();
            paramsrpt.Add(new InfoParamRpt("@dfec_ini", dtpFecIni.Value));
            paramsrpt.Add(new InfoParamRpt("@dfec_fin", dtpFecFin.Value));

            //verificar segun tipo de formato    
            //llamar al rpt segun el tipo de documento
            try
            {
                if (rbtDetallado.Checked)
                {
                    crpReporte.ReportSource = Util.setRptParams(RptRegistroVentas, paramsrpt);
                }
                else if (rbtDetalladoFormato2.Checked)
                {
                    crpReporte.ReportSource = Util.setRptParams(RptRegistroVentasFormato2, paramsrpt);
                }
                else if (rbtResumenDia.Checked)
                {

                    crpReporte.ReportSource = Util.setRptParams(RptRepRegVentasResumen, paramsrpt);
                }
                else
                {
                    //crear la cadena para los grupos de venta
                    lcom_GrupoVenta lcom_grupoventa = new lcom_GrupoVenta();

                    DataTable dtgrpvta = lcom_grupoventa.consultaGrupoVenta("", 1);
                    String ccods_grpvta = "";
                    int ccod_grpvta;
                    if (dtgrpvta.Rows.Count > 0)
                    {
                        //comenzar a recorrer
                        for (int i = 0; i < dtgrpvta.Rows.Count; i++)
                        {
                            ccod_grpvta = Convert.ToInt32(Util.getValueDT("ccod_grpvta", i, dtgrpvta));
                            ccods_grpvta = ccods_grpvta + ccod_grpvta.ToString() + ",";
                        }
                        //al final quitar la ultima coma
                    }

                    ccods_grpvta = ccods_grpvta.Substring(0, ccods_grpvta.Length - 1);

                    paramsrpt.Add(new InfoParamRpt("@ccod_zona", 0));
                    paramsrpt.Add(new InfoParamRpt("@ccods_grpvta", ccods_grpvta));
                    paramsrpt.Add(new InfoParamRpt("@ccod_ruta", 0));
                    crpReporte.ReportSource = Util.setRptParams(RptVentasTotales, paramsrpt);
                }
                tblExportar.Enabled = true; 
            }
            catch (Exception ex)
            {
                tblExportar.Enabled = false;
            }

        }

        private void tstComandos_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            int indice = tstComandos.Items.IndexOf(e.ClickedItem);
            switch (indice)
            {
                case 0:     //nuevo
                    {
                        //nuevaBusqueda();
                        break;
                    }
                case 1:     //imprimir o mostrar
                    {
                        CargarReporte();
                        break;
                    }
                case 2:
                    {
                        if (rbtDetallado.Checked)
                        {
                            Util.exportReporte(RptRegistroVentas);
                        }
                        else if (rbtResumenDia.Checked)
                        {
                            Util.exportReporte(RptRepRegVentasResumen);
                        }
                        else
                        {
                            Util.exportReporte(RptVentasTotales);
                        }
                        break;
                    }
            }
        }

    }
}